package com.parking.mgr.common.pojo.page;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.PageUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.List;
import java.util.function.Function;

/**
 * @ClassName PageResult
 * @Description TODO
 * @author: zhijianpu
 * @Date: 2025/2/28
 */
@Data
@ApiModel(description = "分页结果集")
public class PageResult<T> {

    private static final long serialVersionUID = -6762063665137301L;

    /**
     * 默认分页彩虹展示数量
     */
//    @ApiModelProperty(value = "默认分页展示数量")
//    public static final int RAINBOW_NUM = 5;


    /**
     * 当前页
     */
    @ApiModelProperty(value = "当前页")
    private Integer currPage = 1;

    /**
     * 每页条数
     */
    @ApiModelProperty(value = "每页条数")
    private Integer pageSize = 20;

    /**
     * 总页数
     */
    @ApiModelProperty(value = "总页数")
    private Integer totalPage = 0;

    /**
     * 总记录数
     */
    @ApiModelProperty(value = "总记录数")
    private Integer totalRow = 0;

    /**
     * 上一页
     */
    @ApiModelProperty(value = "上一页")
    private Integer prevPage;

    /**
     * 下一页
     */
    @ApiModelProperty(value = "下一页")
    private Integer nextPage;

    /**
     * 结果集
     */
//    @ApiModelProperty(value = "结果集")
//    private List<T> rows;

    /**
     * 分页彩虹
     */
//    private int[] rainbow;


    public PageResult() {
    }

    /**
     * 将mybatis-plus的page转成自定义的PageResult，扩展了totalPage总页数，和rainBow彩虹条
     */
    public PageResult(Page<T> page) {
        //this.setRows(page.getRecords());
        this.setTotalRow(Convert.toInt(page.getTotal()));
        Integer currPage = Convert.toInt(page.getCurrent());
        this.setCurrPage(currPage);
        if (page.hasPrevious()) {
            this.setPrevPage(currPage - 1);
        } else {
            this.setPrevPage(currPage);
        }
        if (page.hasNext()) {
            this.setNextPage(currPage + 1);
        } else {
            this.setNextPage(currPage);
        }
        if (page.getSize() == 0) {
            this.setCurrPage(0);
        }
        this.setPageSize(Convert.toInt(page.getSize()));
        this.setTotalPage(PageUtil.totalPage(Convert.toInt(page.getTotal()),
                Convert.toInt(page.getSize())));
//        this.setRainbow(PageUtil.rainbow(Convert.toInt(page.getCurrent()),
//                Convert.toInt(this.getTotalPage()), RAINBOW_NUM));
    }

    public <E> PageResult(Page<E> page, Function<E, T> mapper) {
        List<T> rows = page.getRecords().stream().map(mapper).toList();
        //this.setRows(rows);
        this.setTotalRow(Convert.toInt(page.getTotal()));
        Integer currPage = Convert.toInt(page.getCurrent());
        this.setCurrPage(currPage);
        this.setNextPage(currPage + 1);
        this.setPrevPage(currPage - 1);
        this.setPageSize(Convert.toInt(page.getSize()));
        this.setTotalPage(PageUtil.totalPage(Convert.toInt(page.getTotal()),
                Convert.toInt(page.getSize())));
//        this.setRainbow(PageUtil.rainbow(Convert.toInt(page.getCurrent()),
//                Convert.toInt(this.getTotalPage()), RAINBOW_NUM));
    }

    /**
     * 将mybatis-plus的page转成自定义的PageResult，扩展了totalPage总页数，和rainBow彩虹条
     * 可单独设置rows
     */
    public PageResult(Page<T> page, List<T> t) {
        //this.setRows(t);
        this.setTotalRow(Convert.toInt(page.getTotal()));
        Integer currPage = Convert.toInt(page.getCurrent());
        this.setCurrPage(currPage);
        this.setNextPage(currPage + 1);
        this.setPrevPage(currPage - 1);
        this.setPageSize(Convert.toInt(page.getSize()));
        this.setTotalPage(PageUtil.totalPage(Convert.toInt(page.getTotal()),
                Convert.toInt(page.getSize())));
//        this.setRainbow(PageUtil.rainbow(Convert.toInt(page.getCurrent()),
//                Convert.toInt(this.getTotalPage()), RAINBOW_NUM));
    }

}
